package com.study.www.web;

import com.study.www.config.IdWorks;
import com.study.www.config.Result;
import com.study.www.config.WebLog;
import com.study.www.test1.entity.Dept;
import com.study.www.test1.entity.DeptRepository;
import com.study.www.test2.entity.Logger;
import com.study.www.test2.entity.LoggerRepository;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController()
@RequestMapping("/dept")
public class DeptController {


    @Autowired
    DeptRepository deptRepository;
    @Autowired
    LoggerRepository loggerRepository;

    @WebLog("部门查询")
    @GetMapping(value = {"/{id}", ""})
    public Result get(@PathVariable(required = false) String id) {
        if (StringUtils.isBlank(id)) {
            List<Dept> all = deptRepository.findAll();
            return Result.success(all);
        } else {
            return Result.success(deptRepository.findById(id));
        }
    }

    @WebLog("部门新增")
    @PostMapping(value = "")
    @Transactional
    public Result saveDept(@RequestBody Dept dept) {
        dept.setId(IdWorks.getId());
        dept.setCreateTime(new Date());
        deptRepository.save(dept);
        return Result.success();
    }
}
